<?php

namespace app\utils\business;

use think\Model;

/**
 *
 * @mixin Model
 */
class BaseModel extends \app\common\model\BaseModel
{
    protected $autoWriteTimestamp = 'datetime';
    
    protected string $searchField = '';
    protected string $searchFieldMode = 'like';
    
    /**
     * 获取列表数据
     * @param array $param
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function getList($param = [])
    {
        // 查询列表数据
        return $this->setListQueryWhere($param)
            ->paginate(15, false, [
                'query' => \request()->request()
            ]);
    }
    
    /**
     * 获取所有校区列表
     * @param array $param
     * @return false|\PDOStatement|string|\think\Collection
     */
    public static function getAllList($param = [])
    {
        return (new static)->setListQueryWhere($param)->select();
    }
    
    /**
     * 设置列表查询条件
     * @param array $param
     * @return $this
     */
    private function setListQueryWhere($param = [])
    {
        // 查询参数
        $param = array_merge(['search' => '', 'status' => null,], $param);
        if (!empty($param['search']) && !empty($this->searchField)) {
            if ($this->searchFieldMode == 'like') {
                $this->where($this->searchField, 'like', "%{$param['search']}%");
            } elseif ($this->searchFieldMode == '=') {
                $this->where($this->searchField, '=', $param['search']);
            }
        }
        is_numeric($param['status']) && $this->where('status', '=', (int)$param['status']);
        if ($this->fieldExists('sort')) {
            return $this->order(['sort' => 'asc', 'create_time' => 'desc']);
        }
        return $this->order(['create_time' => 'desc']);
    }
    
    /**
     * 新增记录
     * @param $data
     * @return bool
     * @throws \Exception
     */
    public function add($data): bool
    {
        if (!$this->validateForm($data)) {
            return false;
        }
        return $this->allowField(true)->save($this->createData($data));
    }
    
    
    /**
     * 编辑记录
     * @param $data
     * @return false|int
     */
    public function edit($data)
    {
        if (!$this->validateForm($data)) {
            return false;
        }
        return $this->allowField(true)->save($this->createData($data)) !== false;
    }
    
    /**
     * 软删除
     * @return false|int
     */
    public function setDelete()
    {
        return $this->save(['is_delete' => 1]);
    }
    
    /**
     * 创建数据
     * @param array $data
     * @return array
     */
    private function createData(array $data): array
    {
        $data['wxapp_id'] = self::$wxapp_id;
        return $data;
    }
    
    /**
     * 表单验证
     *
     * @return bool
     */
    private function validateForm($data)
    {
        return true;
    }
    
    
    
    /**
     * 详情
     * @param $id
     * @return static|Model
     */
    public static function detail($id)
    {
        /* @var $model self */
        $model = new static;
        $model = $model->where($model->getPk(), '=', $id)
            ->find();
        if (empty($model)) {
            return $model;
        }
        // 整理商品数据并返回
        return $model;
    }
    
    /**
     * 检查是否存在指定的字段名
     * @param string $field 字段名
     * @return bool
     */
    public function fieldExists(string $field): bool
    {
        return in_array($field,$this->getTableFields());
    }
}